// src/pages/task/result/components/ResultTable.tsx - 结果表格组件
import React from 'react';
import { Table, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { ResultItem } from '../types';
import ActionButtons from './ActionButtons';

interface ResultTableProps {
  results: ResultItem[];
  loading: boolean;
  pagination: {
    page: number;
    pageSize: number;
    total: number;
  };
  onPageChange: (pager: any) => void;
  onViewDetail: (record: ResultItem) => void;
}

// 根据任务状态获取标签颜色
const getStatusColor = (status: string) => {
  switch (status) {
    case 'SUCCESS':
      return 'green';
    case 'FAILURE':
      return 'red';
    case 'PENDING':
      return 'orange';
    case 'STARTED':
      return 'blue';
    default:
      return 'default';
  }
};

const ResultTable: React.FC<ResultTableProps> = ({
  results,
  loading,
  pagination,
  onPageChange,
  onViewDetail
}) => {
  const columns: ColumnsType<ResultItem> = [
    {
      title: '任务ID',
      dataIndex: 'task_id',
      key: 'task_id',
      align: 'center',
    },
    {
      title: '调度ID',
      dataIndex: 'schedule_id',
      key: 'schedule_id',
      render: (text) => text || '-',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: (text) => (
        <Tag color={getStatusColor(text)} style={{ margin: '0 auto' }}>
          {text}
        </Tag>
      ),
      align: 'center',
    },
    {
      title: '开始时间',
      dataIndex: 'start_at',
      key: 'start_at',
      render: (text) => text || '-',
      align: 'center',
    },
    {
      title: '完成时间',
      dataIndex: 'finish_at',
      key: 'finish_at',
      render: (text) => text || '-',
      align: 'center',
    },
    {
      title: '操作',
      key: 'action',
      width: 150,
      render: (_, record) => (
        <ActionButtons 
          record={record} 
          onViewDetail={onViewDetail}
        />
      ),
      align: 'center',
    },
  ];

  return (
    <Table
      columns={columns}
      dataSource={results}
      loading={loading}
      rowKey="task_id"
      pagination={{
        current: pagination.page,
        pageSize: pagination.pageSize,
        total: pagination.total,
        showSizeChanger: true,
        showQuickJumper: true,
      }}
      onChange={onPageChange}
    />
  );
};

export default ResultTable;